os.loadAPI("/System/API/image")
os.loadAPI("/System/API/windows")
os.loadAPI("/System/API/zip")
os.loadAPI("/System/API/vedroid")
local run = true
local xSize, ySize = term.getSize()
--xSize = 26
local comp = 0
if xSize > 30 then
	comp = 1
end
local xCenter = math.floor(xSize/2)+1
local obj = {}
local page = 1
local maxPage = 5
local instFolder = "/.ds/"..l_login..'/'
local objName = {}
local id = ""
local mods = ""
local pags = 1
local drawBar = true
local storeVersion = '2.0'
local cursor = 0
local history = {}
local step = 0
local posScroll = 0
local bposScrll = 0
local pos = -1
local bpos = -1
local categories = {}
local prevID = {}
local prevFiles = {}
local prevScrl = {}
local elms = 0
local OSversion = vedroid.getOSversion()
local APIlevel = vedroid.getAPIlevel()
prevID[1] = ""
prevFiles[1] = {}
history[1] = 1
prevScrl[1] = {pos,posScroll}
local screen = window.create(term.current(),1,3,xSize-1,ySize-3)

local function setTextColor(col)
	if term.getTextColor() ~= col then
		term.setTextColor(col)
	end
end

local function setBackgroundColor(col)
	if term.getBackgroundColor() ~= col then
		term.setBackgroundColor(col)
	end
end

local function load(path)
	local file = fs.open(path,"r")
	local data = file.readAll()
	file.close()
	return textutils.unserialize(data)
end

local function save(table,path)
	local file = fs.open(path,"w")
	file.write(textutils.serialize(table))
	file.close()
end

local newObj = function(name,x1,y1,x2,y2)
 obj[name] = {}
 obj[name]["x1"] = x1
 obj[name]["y1"] = y1
 obj[name]["x2"] = x2
 obj[name]["y2"] = y2
end

local pastebin = function(paste,filename)
	local file = http.get("http://pastebin.com/raw.php?i="..paste)
	if file then
		file = file.readAll()
		h=fs.open(filename,"w")
		h.write(file)
		h.close()
		return true
	else
		windows.error("Failed to connect to pastebin.com")
		return false
	end
end

local clear = function(color)
	setBackgroundColor(color)
	term.clear()
	term.setCursorPos(1,1)
end

local usualText = function(string,xPos,yPos)
	term.setCursorPos(xPos,yPos)
	term.write(string)
end

local function textPrinter(text,y,colorT,colorB)
	setTextColor(colorT)
	setBackgroundColor(colorB)
	local words = {}
	local pos = 1

	for i = 1, #text do
		if string.sub(text,i,i) ~= " " then
			if words[pos] == nil then
				words[pos] = ""
			end
			words[pos] = words[pos]..string.sub(text,i,i)
		else
			pos = pos + 1
		end
	end

	term.setCursorPos(2,y)
	pos = 2
	for i=1, #words do
		pos = pos + #words[i]
		if pos <= xSize-1 then
			term.write(words[i].." ")
			pos = pos+1
		else
			y = y+1
			term.setCursorPos(2,y)
			term.write(words[i].." ")
			pos = 2 + #words[i]
			elms = elms + 1
		end
	end
end

local sysBar = function()
	while true do
		if drawBar then
			term.redirect(term.native())
			sysbar.header('Store@',colors.lightGray,colors.white)
			sysbar.draw(colors.white,colors.black,noty,true,false)
		end
		sleep(1)
	end
end

local function refresh()
	local files = fs.list('/.ds/sys/Store@.apk/Resources/repo')
	local i = 1
	while files[i] ~= nil do
		if windows.getFileFormat(files[i]) ~= '.inf' then
			for ii=i,#files-1 do
				files[ii]=files[ii+1]
			end
			files[#files] = nil
		else
			i = i+1
		end
	end
	return files
end

local function refreshCat()
	local files  = refresh()
	local cat = {}
	local n = 1
	for i=1,#files do
		local info = load("/.ds/sys/Store@.apk/Resources/repo/"..files[i])
		local find = false
		for ii=1,#cat do
			if info['category'] == cat[ii] then
				find = true
			end
		end
		if not find then
			cat[#cat+1] = info['category']
		end
	end
	return cat
end

local app = function(name,y)
	local info = load("/.ds/sys/Store@.apk/Resources/repo/"..name..".inf")
	image.draw(2,y,"/.ds/sys/Store@.apk/Resources/repo/"..name..".png")
	setBackgroundColor(colors.white)
	setTextColor(colors.lime)
	usualText(name,9,y)
	setTextColor(colors.black)
	usualText(info['desc'],9,y+1)
	setTextColor(colors.lightGray)
	usualText("By "..info['by'],9,y+2)
	mods = " Install "
	if info["code"] then
		if info['vedroidos'] and (OSversion < info['vedroidos']) or info['craftos'] and (string.sub(os.version(),9) < info['craftos']) or info['apilevel'] and (APIlevel < tonumber(info['apilevel'])) then
			setTextColor(colors.lightGray)
			setBackgroundColor(colors.white)
			mods = 'Not supported'
			newObj(name,0,0,0,0)
		else
			setTextColor(colors.white)
			setBackgroundColor(colors.lime)
			local files = fs.list(instFolder)
			for i=1,#files do
				if files[i] == name..".apk" then
					mods=" Update "
					setTextColor(colors.lime)
					setBackgroundColor(colors.white)
				end
			end
			if name == 'Store@' then
				setTextColor(colors.lime)
				setBackgroundColor(colors.white)
				mods = ' Update '
			end
			newObj(name,xSize-1-#mods,y+5,xSize-2,y+5)
		end
	else
		newObj(name,0,0,0,0)
		setTextColor(colors.orange)
		setBackgroundColor(colors.white)
		mods = "Coming soon"
	end
	usualText(mods,xSize-1-#mods,y+3)
	newObj(name..".inf",2,y+2,xSize-1,y+5)
end

local instApp = function(name,ico,x,y)
	image.draw(x,y,ico)
	setBackgroundColor(colors.white)
	setTextColor(colors.black)
	usualText(name,x+7,y+1)
	usualText("    ",x+#name+3,y+1)
	newObj(name,xSize-10,y+5,xSize-2,y+5)
	local size = fs.getSize("/.ds/"..l_login..'/'..name.."/main")
	size = math.floor(size/1024)
	setTextColor(colors.gray)
	usualText("Size: "..size.." KB",x+7,y+2)
	setTextColor(colors.red)
	usualText("Delete",xSize-7,y+3)
end

local delObj = function()
	local files = refresh()
	for i=1,#files-1 do
		obj[files[i]]["x1"]=-1
		obj[files[i]]["y1"]=-1
		obj[files[i]]["x2"]=-1
		obj[files[i]]["y2"]=-1
	end
end

local function seeApps(apps,y)
	for i=1,#apps do
		app(apps[i],i*5-5+y)
	end
end

local function drawNews()
	local news = load("/.ds/sys/Store@.apk/Resources/news.cfg")
	maxPage = #news
	while true do
		if page == 1 then
			local b = pags
			setTextColor(colors.black)
			setBackgroundColor(colors.white)
			usualText('Interesting',xCenter-12,3)
			setTextColor(colors.lightGray)
			for i=1,maxPage do
				usualText(string.char(7),xCenter+12-i,3)
			end
			newObj('news',xCenter-12,3,xCenter+12-1,12)
			if comp == 1 then
				if pags > 1 then
					image.draw(xCenter-12-24,4,'/.ds/sys/Store@.apk/Resources/news/'..news[pags-1]["img"])
				else
					image.draw(xCenter-12-24,4,'/.ds/sys/Store@.apk/Resources/news/'..news[maxPage]["img"])
				end
				if pags < maxPage then
					image.draw(xCenter+12,4,'/.ds/sys/Store@.apk/Resources/news/'..news[pags+1]["img"])
				else
					image.draw(xCenter+12,4,'/.ds/sys/Store@.apk/Resources/news/'..news[1]["img"])
				end
			end
			image.draw(xCenter-12,4,'/.ds/sys/Store@.apk/Resources/news/'..news[pags]["img"])
			setTextColor(term.getBackgroundColor())
			setBackgroundColor(colors.white)
			usualText(string.char(7),xCenter+12-maxPage+pags-1,3)
			setTextColor(colors.black)
			usualText('<',1,7-4*comp)
			usualText('>',xSize,7-4*comp)
			setBackgroundColor(colors.lime)
			setTextColor(colors.white)
			usualText(" Learn More ",xCenter,10)
			for i=1,10 do
				if b == pags then
					sleep(0.5)
				else
					break
				end
			end
			if b == pags then
				if maxPage > pags then
					pags = pags+1
				else
					pags = 1
				end
			end
		else
			sleep(0.5)
		end
	end
end

local redraw = function()
	if page == 1 then
		clear(colors.white)
		history = {}
		history[1] = 1
		setTextColor(colors.black)
		usualText('See all apps',2,13)
		usualText('Categories',2,15)
		usualText('Installed apps',2,17)
	--[[elseif page == 2 then
		clear(colors.white)
		local new = load('/.ds/sys/Store@.apk/Resources/news.cfg')
		if #new[screen] > 1 then
			seeApps(new[screen],3)
		else
			appinfo(new[screen][1])
		end]]
	elseif page == 3 then
		term.redirect(screen)
		clear(colors.white)
		local ender = math.floor((-pos-1)/5+ySize/5)
		if ender > #files then
			ender = #files
		end
		for i=math.floor((-pos-1)/5)+1,ender do
			local info = load("/.ds/sys/Store@.apk/Resources/repo/"..files[i])
			app(info['name'],i*5+pos-2)
		end
		term.redirect(term.native())
		step = sysbar.scrollBar(#files,5,posScroll,2)
	elseif page == 4 then
		elms = 6
		clear(colors.white)
		sysbar.header('Store@',colors.lightGray,colors.white)
		sysbar.draw(colors.white,colors.black,noty,true,false)
		cursor = 0
		local info = load("/.ds/sys/Store@.apk/Resources/repo/"..id..".inf")
		image.draw(2,pos + 4,"/.ds/sys/Store@.apk/Resources/repo/"..id..".png")
		setBackgroundColor(colors.white)
		setTextColor(colors.black)
		usualText(id,10,pos + 5)
		usualText('>',xSize-1,pos + 9)
		usualText('>',xSize-1,pos + 11)
		setTextColor(colors.lightGray)
		usualText("Category: "..info['category'],2,pos + 9)
		usualText("Author: "..info['by'],2,pos + 11)
		mods = ' Install '
		if info['code'] then
			if info['vedroidos'] and (OSversion < info['vedroidos']) or info['craftos'] and (string.sub(os.version(),9) < info['craftos']) or info['apilevel'] and (APIlevel < tonumber(info['apilevel'])) then
				setTextColor(colors.red)
				setBackgroundColor(colors.white)
				mods = 'Not supported'
				newObj('info',0,0,0,0)
			else
				local files = fs.list(instFolder)
				setBackgroundColor(colors.lime)
				setTextColor(colors.white)
				for i=1,#files do
					if files[i] == id..".apk" then
						mods=" Update "
						setTextColor(colors.lime)
						setBackgroundColor(colors.white)
					end
				end
				if id == 'Store@' then
					setTextColor(colors.lime)
					setBackgroundColor(colors.white)
					mods = ' Update '
				end
				usualText(mods,xSize-1-#mods,pos + 7)
				newObj('info',xSize-1-#mods,pos + 7,xSize-3,pos + 7)
			end
		else
			newObj('info',0,0,0,0)
			setTextColor(colors.orange)
			setBackgroundColor(colors.white)
			mods = "Coming soon"
		end
		usualText(mods,xSize-1-#mods,pos + 7)
		if info['about'] ~= nil then
			textPrinter(info['about'], pos + 13, colors.black, colors.white)
		end
		if info['vedroidos'] ~= nil then
			local x, y = term.getCursorPos()
			term.setCursorPos(2,y+2)
			setTextColor(colors.lightGray)
			if OSversion < info['vedroidos'] then
				setTextColor(colors.red)
			else
				setTextColor(colors.lightGray)
			end
			term.write("VedroidOS version: "..info['vedroidos'])
			elms = elms + 1
		end
		if info['craftos'] ~= nil then
			local x, y = term.getCursorPos()
			term.setCursorPos(2,y+2)
			if string.sub(os.version(),9) < info['craftos'] then
				setTextColor(colors.red)
			else
				setTextColor(colors.lightGray)
			end
			term.write("CraftOS version: "..info['craftos'])
			elms = elms + 1
		end
		if info['apilevel'] ~= nil then
			local x, y = term.getCursorPos()
			term.setCursorPos(2,y+2)
			if APIlevel < tonumber(info['apilevel']) then
				setTextColor(colors.red)
			else
				setTextColor(colors.lightGray)
			end
			term.write("API level: "..info['apilevel'])
			elms = elms + 1
		end
		if info['size'] ~= nil then
			local x, y = term.getCursorPos()
			term.setCursorPos(2,y+2)
			setTextColor(colors.lightGray)
			term.write("Size: "..info['size'])
			elms = elms + 1
		end
		step = sysbar.scrollBar(elms-1,2,posScroll,2)
	elseif page == 5 then
		term.redirect(screen)
		clear(colors.white)
		categories = refreshCat()
		setTextColor(colors.black)
		for i=1,#categories do
			usualText(categories[i],2,i*2+pos)
			usualText(">",xSize-1,i*2+pos)
			newObj(categories[i],1,i*2+pos-2,xSize,i*2+pos)
		end
		term.redirect(term.native())
		step = sysbar.scrollBar(#categories-1,2,posScroll,2)
	elseif page == 6 then
		term.redirect(screen)
		clear(colors.white)
		files = fs.list('/.ds/'..l_login)
		local ender = math.floor((-pos-1)/5+ySize/5)
		if ender > #files then
			ender = #files
		end
		for i=math.floor((-pos-1)/5)+1,ender do
			instApp(files[i],"/.ds/"..l_login..'/'..files[i].."/Resources/icon.png",1,i*5+pos-2)
		end
		term.redirect(term.native())
		step = sysbar.scrollBar(#files,5,posScroll,2)
	end
	sysbar.header('Store@',colors.lightGray,colors.white)
	sysbar.draw(colors.white,colors.black,noty,true,false)
end

local appinfo = function(name)
	--files = {}
	--files[1] = name..'.inf'
	prevScrl[#prevScrl+1] = {}
	prevScrl[#prevScrl][1] = pos
	prevScrl[#prevScrl][2] = posScroll
	posScroll = 0
	pos = -1
	if #prevID > 0 then
		prevID[#prevID+1] = id
	else
		prevID[1] = id
	end
	id = name
	page = 4
	history[#history+1] = page
	redraw()
end

local showCat = function(name)
	local fil = refresh()
	local j = 1
	local inf = ""
	prevFiles[#prevFiles+1] = files
	files = {}
	for i=1,#fil do
		inf = load("/.ds/sys/Store@.apk/Resources/repo/"..fil[i])
		if inf["category"] == name then
			files[j] = fil[i]
			j = j + 1
		end
	end
	page = 3
	prevScrl[#prevScrl+1] = {}
	prevScrl[#prevScrl][1] = pos
	prevScrl[#prevScrl][2] = posScroll
	pos = -1
	posScroll = 0
	history[#history+1] = page
	redraw()
end

local showAuth = function(name)
	local fil = refresh()
	local j = 1
	local inf = ""
	prevFiles[#prevFiles+1] = files
	files = {}
	for i=1,#fil do
		inf = load("/.ds/sys/Store@.apk/Resources/repo/"..fil[i])
		if inf["by"] == name then
			files[j] = fil[i]
			j = j + 1
		end
	end
	page = 3
	prevScrl[#prevScrl+1] = {}
	prevScrl[#prevScrl][1] = pos
	prevScrl[#prevScrl][2] = posScroll
	pos = -1
	posScroll = 0
	history[#history+1] = page
	redraw()
end

local inst = function(name)
	page = 7
	local info = load("/.ds/sys/Store@.apk/Resources/repo/"..name)
	--shell.run("rm /.ds/"..l_login.."/"..info['name']..".apk")
	clear(colors.white)
	setTextColor(colors.black)
	usualText("Installing "..info['name'],xCenter-11,ySize-6)
	usualText("Please wait...",xCenter-11,ySize-5)
	paintutils.drawLine(xCenter-11,ySize-2,xCenter+12,ySize-2,colors.gray)

	paintutils.drawPixel(xCenter-11,ySize-2,colors.lime)

	if pastebin(info['code'],"/.ds/"..l_login.."/"..info['name']..".apk/main") then
		sleep(0.7)

		paintutils.drawLine(xCenter-11,ySize-2,xCenter,ySize-2,colors.lime)
		fs.makeDir('/.ds/'..l_login..'/'..info['name']..'.apk/Resources')

		paintutils.drawLine(xCenter-11,ySize-2,xCenter+5,ySize-2,colors.lime)
		local f = fs.list('/.ds/'..l_login..'/'..info['name']..'.apk/Resources')
		for i=1,#f do
			if f[i] == 'icon.png' then
				fs.delete('/.ds/'..l_login..'/'..info['name']..'.apk/Resources/icon.png')
				break
			end
		end
		fs.copy('/.ds/sys/Store@.apk/Resources/repo/'..info['name']..'.png', '/.ds/'..l_login..'/'..info['name']..'.apk/Resources/icon.png')

		if info['res'] then
			for i=1,#info['res'] do
				pastebin(info['res'][i]['code'],'/.ds/'..l_login..'/'..info['name']..'.apk/Resources/'..info['res'][i]['name'])
			end
		end

		if info['adds'] then
			for i=1,#info['adds'] do
				pastebin(info['adds'][i]['code'],'/.ds/'..l_login..'/'..info['name']..'.apk/'..info['adds'][i]['name'])
			end
		end

		if info['api'] then
			for i=1,#info['api'] do
				pastebin(info['api'][i]['code'],info['api'][i]['name'])
			end
		end

		paintutils.drawLine(xCenter-11,ySize-2,xCenter+12,ySize-2,colors.lime)
		sleep(0.5)

		if noty ~= nil then
			noty[#noty+1]={["app"]=info['name'], ["text"]=info['name'].." installed", ["button"]=" Launch ", ["ico"]=string.char(25)}
		else
			noty[1]={["app"]=info['name'], ["text"]=info['name'].." installed", ["button"]=" Launch ", ["ico"]=string.char(25)}
		end
	end

	page = history[#history]
	if page == 4 then
		appinfo(info['name'])
	else
		redraw()
	end
end

local running = function()
	while run == true and l_login ~= '...' and l_login ~= nil and l_login ~= '' do
		event, side, x, y = os.pullEvent()
		if event == "mouse_scroll" then
			if side == 1 then
				if page == 3 or page == 6 or page == 5 then
					if (pos>-#files*5+ySize-5 and (page == 3 or page == 6)) or (pos > -#categories*2+ySize-2 and page == 5) then
						posScroll = posScroll-step
						pos = pos-2
						redraw()
					else
						if page == 3 or page == 6 then
							pos = -#files*5+ySize-5
						else
							pos = -#categories*2+ySize-2
						end
					end
				--[[else
					screen = screen+1
					if screen > maxScreen then
						screen = maxScreen
					else
						clear(colors.white)
						redraw()
					end]]
				elseif page == 4 then
					if pos>-elms*2+ySize-2 then
						posScroll = posScroll-step
						pos = pos-2
						redraw()
					else
						pos = -elms*2+ySize-2
					end
				end
			elseif side == -1 then
				if page == 3 or page == 6 or page == 5 or page == 4 then
					if pos<-1 then
						pos = pos+2
						posScroll = posScroll+step
						if pos > -1 then
							pos = -1
						end
						redraw()
					end
				--[[else
					screen = screen-1
					if screen < 1 then
						screen = 1
					else
						clear(colors.white)
						redraw()
					end]]
				end
			end
		--[[elseif event == "key" then
			if side == 208 then
				screen = screen+1
				if screen > maxScreen then
					screen = maxScreen
				else
					clear(colors.white)
					redraw()
				end
			elseif side == 200 then
				screen = screen-1
				if screen < 1 then
					screen = 1
				else
					clear(colors.white)
					redraw()
				end
			end]]
		elseif event == "mouse_click" then
			if sysbar.back(x,y) == true then
				if #history > 1 then
					x = 0
					y = 0
					if page == 4 then
						id = prevID[#prevID]
						prevID[#prevID] = nil
						--[[pos = bpos
						bpos = -1
						posScroll = bposScrll
						bposScrll = 0]]
					elseif page == 3 or page == 6 then
						if #prevFiles > 0 then
							files = prevFiles[#prevFiles]
						end
						prevFiles[#prevFiles] = nil
					end
					page = history[#history-1]
					history[#history] = nil
					pos = prevScrl[#prevScrl][1]
					posScroll = prevScrl[#prevScrl][2]
					prevScrl[#prevScrl] = nil
					redraw()
				else
					term.clear()
					term.setCursorPos(1,1)
					run = false
				end
			elseif sysbar.home(x,y) == true then
				term.clear()
				term.setCursorPos(1,1)
				home = true
				run = false
			elseif page == 1 then
				if x==1 and y==7-4*comp then
					setBackgroundColor(colors.lightGray)
					setTextColor(colors.white)
					usualText('<',1,7-4*comp)
					pags = pags - 1
					if pags < 1 then
						pags = maxPage
					end
				elseif x==xSize and y==7-4*comp then
					setBackgroundColor(colors.lightGray)
					setTextColor(colors.white)
					usualText('>',xSize,7-4*comp)
					pags = pags + 1
					if pags > maxPage then
						pags = 1
					end
				elseif y==13 then
					paintutils.drawLine(1,13,xSize,13,colors.lightGray)
					setTextColor(colors.white)
					usualText('See all apps',2,13)
					prevFiles[#prevFiles] = files
					page = 3
					prevScrl[#prevScrl+1] = {}
					prevScrl[#prevScrl][1] = pos
					prevScrl[#prevScrl][2] = posScroll
					pos = -1
					posScroll = 0
					files = refresh()
					history[#history+1] = page
					sleep(0.1)
					redraw()
				elseif y==15 then
					paintutils.drawLine(1,15,xSize,15,colors.lightGray)
					setTextColor(colors.white)
					usualText('Categories',2,15)
					prevScrl[#prevScrl+1] = {}
					prevScrl[#prevScrl][1] = pos
					prevScrl[#prevScrl][2] = posScroll
					pos = -1
					posScroll = 0
					page = 5
					history[#history+1] = page
					sleep(0.1)
					redraw()
				elseif y==17 then
					paintutils.drawLine(1,17,xSize,17,colors.lightGray)
					setTextColor(colors.white)
					usualText('Installed apps',2,17)
					page = 6
					prevScrl[#prevScrl+1] = {}
					prevScrl[#prevScrl][1] = pos
					prevScrl[#prevScrl][2] = posScroll
					pos = -1
					posScroll = 0
					history[#history+1] = page
					sleep(0.1)
					redraw()
				elseif x>=obj["news"]["x1"] and x<=obj["news"]["x2"] and y>=obj["news"]["y1"] and y<=obj["news"]["y2"] then
					local new = load('/.ds/sys/Store@.apk/Resources/news.cfg')
					if #new[pags]['apps'] > 1 then
						prevFiles[#prevFiles + 1] = files
						page = 3
						prevScrl[#prevScrl+1] = {}
						prevScrl[#prevScrl][1] = pos
						prevScrl[#prevScrl][2] = posScroll
						pos = -1
						posScroll = 0
						files = {}
						for i = 1, #new[pags]['apps'] do
							files[i] = new[pags]['apps'][i]..".inf"
						end
						history[#history+1] = page
						redraw()
					else
						appinfo(new[pags]['apps'][1])
					end
				end
			elseif page == 3 then
				--files = refresh()
				for i=1,#files do
					local info = load("/.ds/sys/Store@.apk/Resources/repo/"..files[i])
					if x>=obj[info['name']]["x1"] and x<=obj[info['name']]["x2"] and y>=obj[info['name']]["y1"] and y<=obj[info['name']]["y2"] then
						inst(files[i])
						break
					elseif x>=obj[files[i]]["x1"] and x<=obj[files[i]]["x2"] and y>=obj[files[i]]["y1"] and y<=obj[files[i]]["y2"] then
						appinfo(info['name'])
						--history[#history+1] = 4
						break
					end
				end
			elseif page == 4 then
				if x>=obj['info']["x1"] and x<=obj['info']["x2"] and y>=obj['info']["y1"] and y<=obj['info']["y2"] then
					inst(id..".inf")
				elseif y==pos + 9 then
					local info = load("/.ds/sys/Store@.apk/Resources/repo/"..id..".inf")
					paintutils.drawLine(1,pos + 9,xSize,pos + 9,colors.lightGray)
					setTextColor(colors.white)
					usualText("Category: "..info['category'],2,pos + 9)
					sleep(0.1)
					showCat(info["category"])
				elseif y==pos + 11 then
					local info = load("/.ds/sys/Store@.apk/Resources/repo/"..id..".inf")
					paintutils.drawLine(1,pos + 11,xSize,pos + 11,colors.lightGray)
					setTextColor(colors.white)
					usualText("Author: "..info['by'],2,pos + 11)
					sleep(0.1)
					showAuth(info["by"])
				end
			elseif page == 5 then
				for i=1,#categories do
					if y == i*2 + 1 then
						paintutils.drawLine(1,i*2 + 1,xSize,i*2 + 1,colors.lightGray)
						setTextColor(colors.white)
						usualText(categories[i],2,i*2 + 1)
						sleep(0.1)
						showCat(categories[i])
						break
					end
				end
			elseif page == 6 then
				files = fs.list('/.ds/'..l_login)
				local ender = math.floor((-pos-1)/5+ySize/5)
			  if ender > #files then
			 	 ender = #files
			  end
				for i=math.floor((-pos-1)/5)+1,ender do
					if x>=obj[files[i]]["x1"] and x<=obj[files[i]]["x2"] and y>=obj[files[i]]["y1"] and y<=obj[files[i]]["y2"] then
						local but = windows.select({"Delete"},{"Are you sure?"},{"Yes",colors.red,colors.white},{"No",colors.gray,colors.white})
						if but == "Yes" then
							fs.delete("/.ds/"..l_login..'/'..files[i])
							redraw()
							break
						elseif but == "No" then
							redraw()
						end
					end
				end
			end
		end
	end
end

local start = function()
	redraw()
	running()
end

clear(colors.white)
setTextColor(colors.black)
usualText("Loading...",xSize-10,19)
pastebin("42ujyRYf","/.ds/sys/Store@.apk/Resources/repo.zip")
zip.unarchive("/.ds/sys/Store@.apk/Resources/repo.zip","/")
fs.delete("/.ds/sys/Store@.apk/Resources/repo.zip")
if l_login == '...' or l_login == nil or l_login == '' then
	pastebin("AH3k8DMa","/System/ver.sec")
	shell.run('/System/ver.sec')
end
parallel.waitForAny(start,drawNews, sysBar)
clear(colors.black)